package com.anXin.article.controller;



import com.anXin.article.service.ArticleService;
import com.anXin.article.service.CollectService;
import com.anXin.model.article.pojos.Article;
import com.anXin.model.article.pojos.Collect;
import com.anXin.model.common.dtos.ResponseResult;

import com.anXin.model.user.pojos.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/app/collect")
public class CollectController {


    @Resource
    private CollectService collectService;

    @Resource
    private ArticleService articleService;

    /**
     * 添加收藏√
     * @param articleId
     * @return
     */

    @PostMapping("/add/{articleId}")
    public ResponseResult Collect(@PathVariable Long articleId){
   User user=AppThreadLocalUtil.getUser();

//       User user=new User();
//       user.setId(1302865306489733122L);

        Collect collect=new Collect();
        collect.setArticleId(articleId);
        collect.setUserId(user.getId());
        Article article=articleService.query().eq("id",articleId).one();
        collect.setBlogTitle(article.getTitle());
        collectService.save(collect);
        return ResponseResult.okResult(collect);

    }


    /**
     * 取消收藏√
     * @param articleId
     */

    @DeleteMapping("/delete/{articleId}")
    public ResponseResult Remove(@PathVariable("articleId") Long articleId){
        User user=AppThreadLocalUtil.getUser();

//        User user=new User();
//        user.setId(1302865306489733123L);//测试时把次注解与上方User线程获取互换，设定一个一个死的user_id


        Collect collect=collectService.query().eq("article_id",articleId).eq("user_id",user.getId()).one();
        collectService.removeById(collect.getId());
        return ResponseResult.okResult("删除成功");
    }

    /**
     * 查询收藏√
     * @return
     */

    @GetMapping("/content")
    public ResponseResult queryCollect(){
        List<Collect> list = collectService.query().eq("user_id", AppThreadLocalUtil.getUser().getId()).list();
        for (Collect collect : list){
            Article article=articleService.query().eq("id",collect.getArticleId()).one();
            collect.setBlogTitle(article.getTitle());

        }

        return ResponseResult.okResult(list);
    }

}
